
Partial Class Case_RepGuardianPermissions
    Inherits System.Web.UI.UserControl

    Public ReadOnly Property PrimaryObject() As Cdm.DataCore.UserSecurity.IUserPermissionEntity
        Get
            Dim parentControl As IPermission = Nothing
            If TypeOf (Me.Parent) Is IPermission Then
                parentControl = Me.Parent
            ElseIf TypeOf Me.Parent.Parent Is IPermission Then
                parentControl = Me.Parent.Parent
            End If
            If Not parentControl Is Nothing Then
                Return parentControl.PrimaryObject
            End If
            Return Nothing
        End Get
    End Property
    
    Private ReadOnly Property CurrentPermission() As Cdm.DataCore.UserSecurity.Permission.PermissionType
        Get
            If TypeOf (Me.PrimaryObject) Is Cdm.DataCore.Entities.LegalGuardian Then
                Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseGuardianPermissions
            ElseIf TypeOf (Me.PrimaryObject) Is Cdm.DataCore.Entities.ParticipantRepresentative Then
                Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseRepPermissions
            Else
                Return Nothing
            End If
        End Get
    End Property

    Private ReadOnly Property CurrentRole() As Cdm.DataCore.UserSecurity.Role.RoleType
        Get
            If TypeOf (Me.PrimaryObject) Is Cdm.DataCore.Entities.LegalGuardian Then
                Return Cdm.DataCore.UserSecurity.Role.RoleType.LegalGuardian
            ElseIf TypeOf (Me.PrimaryObject) Is Cdm.DataCore.Entities.ParticipantRepresentative Then
                Return Cdm.DataCore.UserSecurity.Role.RoleType.Representative
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public Function PermissionIDList() As System.Collections.Generic.List(Of Cdm.DataCore.UserSecurity.Permission)
        Return Cdm.DataCore.UserSecurity.Permission.ListForRepsAndGuardians()
    End Function
    Public Function AllPermissionList() As System.Collections.Generic.List(Of Cdm.DataCore.UserSecurity.Permission)
        Return Cdm.DataCore.UserSecurity.Permission.ListAll
    End Function

    Public Function DisplayPermission(ByVal permID As Integer) As String
        Dim i As Integer
        For i = 0 To AllPermissionList().Count - 1
            If AllPermissionList(i).PermissionId = permID Then
                Return AllPermissionList(i).Name
            End If
        Next
        Return ""
    End Function

    Private Sub SetGridValues()
        Dim userPermissionList As System.Collections.Generic.List(Of Cdm.DataCore.UserSecurity.RepGuardianPermission) = Me.PrimaryObject.PermissionList
        If userPermissionList Is Nothing Then Exit Sub
        For i As Integer = 0 To userPermissionList.Count - 1
            SetSingleValue(userPermissionList(i).PermissionId, userPermissionList(i).MayInsert, _
                            userPermissionList(i).MayView, userPermissionList(i).MayUpdate)
        Next
        Me.PermissionGridView.Enabled = UserAuthorization.HasUpdatePermission(Me.CurrentPermission)
    End Sub

    Private Sub SetSingleValue(ByVal permissionId As Integer, ByVal mayInsert As Boolean, _
                                ByVal mayView As Boolean, ByVal mayUpdate As Boolean)
        Dim hasUpdatePermission As Boolean = UserAuthorization.HasUpdatePermission(Me.CurrentPermission)
        Dim permissionRow As Integer = GetPermissionRow(permissionId)
        If permissionRow > -1 Then
            Dim UpdateCheckBox As CheckBox = PermissionGridView.Rows(permissionRow).FindControl("UpdateCheckBox")
            UpdateCheckBox.Checked = mayUpdate
            UpdateCheckBox.Enabled = hasUpdatePermission
            Dim ViewCheckBox As CheckBox = PermissionGridView.Rows(permissionRow).FindControl("ViewCheckBox")
            ViewCheckBox.Checked = mayView
            ViewCheckBox.Enabled = hasUpdatePermission
            Dim InsertCheckBox As CheckBox = PermissionGridView.Rows(permissionRow).FindControl("InsertCheckBox")
            InsertCheckBox.Checked = mayInsert
            InsertCheckBox.Enabled = hasUpdatePermission
        End If
    End Sub

    Private Function GetPermissionRow(ByVal permissionId As Integer) As Integer
        Dim rowPermissionId As Integer
        For i As Integer = 0 To Me.PermissionGridView.Rows.Count - 1
            rowPermissionId = GetDataKey(i)
            If rowPermissionId = permissionId Then
                Return i
            End If
        Next
        Return -1
    End Function

    Private Function GetDataKey(ByVal rowValue As Integer) As Integer
        Try
            Dim keys As DataKeyArray = Me.PermissionGridView.DataKeys
            Return Convert.ToInt32(keys.Item(rowValue).Values.Item(0))
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Sub SetPermissions()
        Dim permissionId As Integer
        Dim mayUpdate As Boolean
        Dim mayInsert As Boolean
        Dim mayView As Boolean
        Dim chk As CheckBox
        'SetControlPropertiesByPermission()
        For i As Integer = 0 To Me.PermissionGridView.Rows.Count - 1
            permissionId = GetDataKey(i)
            chk = PermissionGridView.Rows(i).FindControl("UpdateCheckBox")
            mayUpdate = chk.Checked
            chk = PermissionGridView.Rows(i).FindControl("InsertCheckBox")
            mayInsert = chk.Checked
            chk = PermissionGridView.Rows(i).FindControl("ViewCheckBox")
            mayView = chk.Checked
            SetSinglePermission(permissionId, mayUpdate, mayInsert, mayView)
        Next
    End Sub
    Private Sub SetSinglePermission(ByVal permissionId As Integer, ByVal mayUpdate As Integer, _
                                    ByVal mayInsert As Integer, ByVal mayView As Integer)
        For i As Integer = 0 To Me.PrimaryObject.PermissionList.Count - 1
            If Me.PrimaryObject.PermissionList(i).PermissionId = permissionId Then
                Me.PrimaryObject.PermissionList(i).MayInsert = mayInsert
                Me.PrimaryObject.PermissionList(i).MayUpdate = mayUpdate
                Me.PrimaryObject.PermissionList(i).MayView = mayView
                Return
            End If
        Next
        'Permission object not found - create a new one
        Dim newPermission As New Cdm.DataCore.UserSecurity.RepGuardianPermission()
        newPermission.PermissionId = permissionId
        newPermission.MayDelete = False
        newPermission.MayInsert = mayInsert
        newPermission.MayUpdate = mayUpdate
        newPermission.MayView = mayView
        newPermission.UserId = Me.PrimaryObject.UserId
        Me.PrimaryObject.PermissionList.Add(newPermission)
    End Sub

    Public Sub BindGridView()
        Me.PermissionGridView.DataSource = PermissionIDList()
        Me.PermissionGridView.DataBind()
        SetGridValues()
    End Sub

    Public Sub CheckRolePermissions()

        Dim viewPermissionCheckBox As CheckBox
        Dim updatePermissionCheckBox As CheckBox
        Dim insertPermissionCheckBox As CheckBox

        Dim row As GridViewRow
        For Each row In Me.PermissionGridView.Rows

            viewPermissionCheckBox = CType(row.Cells(1).Controls(1), CheckBox)
            updatePermissionCheckBox = CType(row.Cells(2).Controls(1), CheckBox)
            insertPermissionCheckBox = CType(row.Cells(3).Controls(1), CheckBox)

            viewPermissionCheckBox.Enabled = UserAuthorization.HasViewPermission(Me.PermissionGridView.DataKeys(row.RowIndex).Values(0))
            updatePermissionCheckBox.Enabled = UserAuthorization.HasUpdatePermission(Me.PermissionGridView.DataKeys(row.RowIndex).Values(0))
            insertPermissionCheckBox.Enabled = UserAuthorization.HasInsertPermission(Me.PermissionGridView.DataKeys(row.RowIndex).Values(0))
            
        Next

    End Sub

    Protected Sub PermissionGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles PermissionGridView.RowDataBound
        Dim viewCheckBox As CheckBox = e.Row.FindControl("ViewCheckBox")
        Dim updateCheckBox As CheckBox = e.Row.FindControl("UpdateCheckBox")
        Dim insertCheckBox As CheckBox = e.Row.FindControl("InsertCheckBox")
        Dim currentPermission As Cdm.DataCore.UserSecurity.Permission = CType(e.Row.DataItem, Cdm.DataCore.UserSecurity.Permission)
        If currentPermission IsNot Nothing Then

            Dim permissionName As String = currentPermission.Name
            If viewCheckBox IsNot Nothing Then
                viewCheckBox.InputAttributes.Add("title", "Read " & permissionName)
            End If
            If updateCheckBox IsNot Nothing Then
                updateCheckBox.InputAttributes.Add("title", "Change " & permissionName)
            End If
            If insertCheckBox IsNot Nothing Then
                insertCheckBox.InputAttributes.Add("title", "Add " & permissionName)
            End If
        End If
    End Sub
End Class
